<template>
	<view class="container">
		<view class="example">
			<uni-forms ref="form" :model="userInfo" labelWidth="80px">
				<uni-forms-item label="联系人" name="signer_name" labelWidth="90px">
					<uni-easyinput v-model="userInfo.signer_name" placeholder="请输入联系人" />
				</uni-forms-item>
				<uni-forms-item label="联系人电话" name="signer_mobile" labelWidth="120px">
					<uni-easyinput maxlength="11" v-model="userInfo.signer_mobile" placeholder="请输入联系人电话" />
				</uni-forms-item>
				<uni-forms-item label="省市区">
					<picker mode="region" @change="bindPickerAddressChange" style="width: 100%;">
						<input class="input-address" type="text" v-model="address" disabled placeholder="请选择省市区">
					</picker>
				</uni-forms-item>
				<uni-forms-item label="详细地址" name="address">
					<uni-easyinput v-model="userInfo.address" placeholder="请输入详细地址" />
				</uni-forms-item>
			</uni-forms>
			<button type="primary" @click="submit" style="background-color: #3c9cff;">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		getUserAddressApi,
		addUserAddressInfoApi,
		editUserAddressInfoApi
	} from '@/api/index.js'
	export default {
		data() {
			return {
				type: 'add',
				userInfo: {
					signer_name: "",
					signer_mobile: "",
					province: "",
					city: "",
					district: "",
					address: "",
					introduction: ""
				},
				address: "",
				rules: {
					sms_name: {
						rules: [{
							required: true,
							errorMessage: '联系人不能为空'
						}]
					},
					signer_mobile: {
						rules: [{
							required: true,
							errorMessage: '联系人电话不能为空'
						}, {
							pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
							errorMessage: '请输入正确的联系人电话'
						}]
					},
				},
			}
		},
		onLoad() {
			this.userInfo = uni.getStorageSync('userInfo')
			this.getUserInfo(this.userInfo.id)
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			// 获取用户信息
			async getUserInfo(id) {
				const {
					data: res
				} = await getUserAddressApi(id);
				// console.log(res);
				if (res.results.length > 0) {
					const results = res.results[0];
					this.userInfo = results;
					this.address = results.province + results.city + results.district;
					this.type = 'edit'
				} else {
					this.type = 'add'
				}
			},
			// 选择地区
			bindPickerAddressChange: function(e) {
				const {
					value
				} = e.detail
				this.address = e.detail.value.join('')
				this.address = value[0] + value[1] + value[2];
				this.userInfo.province = value[0];
				this.userInfo.city = value[1];
				this.userInfo.district = value[2];

			},


			submit(ref) {
				this.$refs.form.validate().then(res => {
					console.log(this.userInfo);
					const {
						signer_name,
						signer_mobile,
						email,
						province,
						city,
						district,
						address,
					} = this.userInfo;
					let params = {
						signer_name,
						signer_mobile,
						district,
						province,
						city,
						address
					}
					if (this.type === 'edit') {
						this.editUserAddressInfo(params)
					} else if (this.type === 'add') {
						this.editUserAddressInfo(params)
					}

				})
			},
			// 修改地址信息
			async editUserAddressInfo(params) {
				const {
					id
				} = this.userInfo;
				params.uid = id;
				// console.log(params);
				if (this.type === 'add') {
					const res = await addUserAddressInfoApi(params)
					console.log(res);
					uni.showToast({
						title: '添加成功'
					})
				} else if (this.type === 'edit') {
					const res = await editUserAddressInfoApi(params)
					console.log(res);
					if (res.statusCode === 200) {
						uni.showToast({
							title: '修改成功'
						})
					}
				}

			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}

	.uni-forms-item__content {
		display: flex;
		align-items: center;
	}

	.uni-easyinput[disabled] {
		opacity: 1 !important;
		cursor: text !important;
		/* 其它样式属性... */
	}

	.input-address {
		box-sizing: border-box;
		flex-direction: row;
		align-items: center;
		border: 1px solid #F0F0F0;
		border-radius: 4px;
		border-color: #e5e5e5;
		background-color: #fff;
		padding-left: 10px;
		font-size: 12px;
		height: 35px;
	}
</style>